﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace System
{
    public static class IsBetweenExtension
    {
        /// <summary>
        /// 是否在界限值内
        /// </summary>
        /// <typeparam name="T">泛型参数</typeparam>
        /// <param name="t">泛型参数</param>
        /// <param name="lowerBound">下边界</param>
        /// <param name="upperBound">上边界</param>
        /// <param name="includeLowerBound">是否包含下边界</param>
        /// <param name="includeUpperBound">是否包含上边界</param>
        /// //int
        ///bool b0 = 3.IsBetween(1, 5);
        ///bool b1 = 3.IsBetween(1, 3, includeUpperBound: true);
        ///bool b2 = 3.IsBetween(3, 5, includeLowerBound: true);
        /////double
        ///bool b3 = 3.14.IsBetween(3.0, 4.0);
        /////string
        ///bool b4 = "ND".IsBetween("NA", "NC");
        /////DateTime
        ///bool b5 = new DateTime(2011, 2, 17).IsBetween(new DateTime(2011, 1, 1), new DateTime(2011, 3, 1));
        /// <returns></returns>
        public static bool IsBetween<T>(this T t, T lowerBound, T upperBound, bool includeLowerBound = false, bool includeUpperBound = false)
        where T : IComparable<T>
        {
            if (t == null) throw new ArgumentNullException("t");

            var lowerCompareResult = t.CompareTo(lowerBound);
            var upperCompareResult = t.CompareTo(upperBound);

            return (includeLowerBound && lowerCompareResult == 0) ||
                (includeUpperBound && upperCompareResult == 0) ||
                (lowerCompareResult > 0 && upperCompareResult < 0);
        }
    }
}
